<?php
	function realstateSuperSelectAjaxSet($name,$parent,$value){
		global $setupdata, $mysqlclass;
		$parent = intval($parent);
		$value = stripslashes($value);
		$name = stripslashes($name);
		$mysqlclass->mysql_my_query("DELETE FROM `".$setupdata['table_prefix']."realstate_data` WHERE `parent`=".$parent." AND `name`='".$name."'", __LINE__, __FILE__);
		$mysqlclass->mysql_my_query("INSERT INTO `".$setupdata['table_prefix']."realstate_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`lang`) VALUES ('', ".$parent.", '".$name."', '".$value."', 'super_select', 0, '".$_SESSION['lang']."')", __LINE__, __FILE__);
	}

	function realstateSuperSelectAjaxDel($value){
		global $setupdata, $mysqlclass;
		$value = intval($value);
		$mysqlclass->mysql_my_query("DELETE FROM `".$setupdata['table_prefix']."object_data` WHERE `id`=".$value, __LINE__, __FILE__);		
	}
	
	function realstateSuperSelectAjaxEdit($parent, $value){
		global $setupdata, $mysqlclass;
		$value = stripslashes($value);
		$parent = intval($parent);
		$sql = $mysqlclass->mysql_my_query("SELECT `value` FROM `".$setupdata['table_prefix']."object_data` WHERE `id`=".$parent, __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			if (($row['value'] != $value) && (strlen($value)>0)){
				$mysqlclass->mysql_my_query("UPDATE `".$setupdata['table_prefix']."object_data` SET `value`='".$value."' WHERE `id`=".$parent, __LINE__, __FILE__);
			}
		}
	}
	
	function realstateSuperSelectAjaxAdd($name,$parent,$value){
		global $setupdata, $mysqlclass;
		$parent = intval($parent);
		$value = stripslashes($value);
		$name = stripslashes($name);
		$mysqlclass->mysql_my_query("SELECT `value` FROM `".$setupdata['table_prefix']."object_data` WHERE `value`='".$value."' AND `name`='".$name."' AND `group`='realstate_super_select'", __LINE__, __FILE__);
		if (mysql_num_rows($sql) == 0) {
			$mysqlclass->mysql_my_query("INSERT INTO `".$setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', ".$parent.", '".$name."', '".$value."', 'text', 0, 'realstate_super_select')", __LINE__, __FILE__);
		}
	}
	
	function realstateSuperSelectAjax($type,$name,$parent=0, $value=""){
		global $realstateclass, $smarty, $xajax;
		$objResponse = new xajaxResponse();
		//$objResponse->addAlert(print_r($type, true)." - ".print_r($name, true)." - ".print_r($parent, true)." - ".print_r($value, true));

		if ($type == 'set'){
			realstateSuperSelectAjaxSet($name,$parent,$value);
		} elseif ($type == 'remove'){
			realstateSuperSelectAjaxDel($value);
		} elseif ($type == 'add'){
			if (!is_array($value)) realstateSuperSelectAjaxAdd($name,$parent,$value);
		} elseif ($type == 'edit'){
			if (!is_array($value)) realstateSuperSelectAjaxEdit($parent,$value);
		}

		$result = $realstateclass->get_super_select(array("name"=>$name,"parent"=>$parent));
	    $content = "<select name='".$name."' id='".$name."_id' style='width:250px' onChange=\"xajax_realstateSuperSelectAjax('set','".$name."','".$parent."',xajax.$('".$name."_id').value);\">\n
			<option value=''></option>\n";
		foreach ($result as $key=>$value){
			if ($key != "selected")
				if ($result['selected']['value'] == $value['value']){
					$content .= "<option value='".$value['value']."' selected>".$value['text']."</option>\n";
				} else {
					$content .= "<option value='".$value['value']."'>".$value['text']."</option>\n";
				}
		}
		$content .= "</select>";
		$objResponse->addAssign("select_".$name."_id", "innerHTML", $content);
		return $objResponse;
	}
?>
